package com.walnut.dao.impl;

import com.walnut.dao.OrderDao;
import com.walnut.pojo.Food;
import com.walnut.pojo.Order;
import com.walnut.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public List<Order> select() throws SQLException {
        List<Order> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = " select * from `order` ";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            Order order=new Order().builder()
                    .goodsName(rs.getString("goods_name"))
                    .goodsId(rs.getInt("goods_id"))
                    .goodsType(rs.getString("goods_type"))
                    .goodsTypeId(rs.getInt("goods_type_id"))
                    .purchaser(rs.getString("purchaser"))
                    .createTime(rs.getDate("create_time"))
                    .price(rs.getDouble("price"))
                    .build();
            list.add(order);
        }
        return list;
    }

    @Override
    public Order selectById(Integer goodsId) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = " select * from `order` where goods_id=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, goodsId);
        rs = pstmt.executeQuery();
        rs.next();
        Order order=new Order().builder()
                .goodsName(rs.getString("goods_name"))
                .goodsType(rs.getString("goods_type"))
                .goodsTypeId(rs.getInt("goods_type_id"))
                .purchaser(rs.getString("purchaser"))
                .createTime(rs.getDate("create_time"))
                .price(rs.getDouble("price"))
                .build();
        return order;
    }

    public int selectByName(String goodsName) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = " select * from `order` where goods_name=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, goodsName);
        rs = pstmt.executeQuery();
        if (rs.next()) return 1;
        else return 0;
    }

    public List<Order> selectByConditions(String goodsName, String price, String goodsType, String loginer) throws SQLException {
        List<Order> orders = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT * FROM `order` WHERE 1=1");

        // 构建查询条件
        if (goodsName != null && !goodsName.isEmpty()) {
            sql.append(" AND goods_name LIKE ?");
        }
        if (price != null && !price.isEmpty()) {
            sql.append(" AND price = ?");
        }
        if (goodsType != null && !goodsType.isEmpty()) {
            sql.append(" AND goods_type = ?");
        }

        if (loginer != null && !loginer.isEmpty()){
            sql.append(" AND purchaser = ?");
        }

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql.toString())) {

            int index = 1;

            // 设置查询参数
            if (goodsName != null && !goodsName.isEmpty()) {
                stmt.setString(index++, "%" + goodsName + "%"); // 模糊匹配食品名称
            }
            if (price != null && !price.isEmpty()) {
                stmt.setDouble(index++, Double.parseDouble(price)); // 精确匹配价格
            }
            if (goodsType != null && !goodsType.isEmpty()) {
                stmt.setString(index++, goodsType); // 精确匹配食品状态
            }
            if (loginer != null && !loginer.isEmpty()){
                stmt.setString(index++, loginer);
            }

            ResultSet rs = stmt.executeQuery();

            // 判断结果集是否为空
            while (rs.next()) {
                Order order = new Order().builder()
                        .goodsId(rs.getInt("goods_id"))
                        .goodsName(rs.getString("goods_name"))
                        .goodsType(rs.getString("goods_type"))
                        .goodsTypeId(rs.getInt("goods_type_id"))
                        .purchaser(rs.getString("purchaser"))
                        .createTime(rs.getDate("create_time"))
                        .price(rs.getDouble("price"))
                        .build();
                orders.add(order);
            }
            return orders;
        }

    }

    @Override
    public int insert(Order order) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        String sql = "INSERT INTO `order` (goods_name, price, goods_type_id, goods_type, purchaser, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?)";


        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, order.getGoodsName());
        pstmt.setDouble(2, order.getPrice());
        pstmt.setInt(3, order.getGoodsTypeId());
        pstmt.setString(4, order.getGoodsType());
        pstmt.setString(5, order.getPurchaser());
        pstmt.setDate(6, new java.sql.Date(order.getCreateTime().getTime()));
        pstmt.setDate(7, new java.sql.Date(order.getUpdateTime().getTime()));

        int count=pstmt.executeUpdate();
        return count;
    }

    @Override
    public int update(Order order) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        String sql = "UPDATE `order` SET goods_name = ?, price = ?, goods_type_id = ?, goods_type = ?, purchaser = ?, create_time = ?, update_time = ? WHERE goods_id = ?";

        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, order.getGoodsName());
        pstmt.setDouble(2, order.getPrice());
        pstmt.setInt(3, order.getGoodsTypeId());
        pstmt.setString(4, order.getGoodsType());
        pstmt.setString(5, order.getPurchaser());
        pstmt.setDate(6, new java.sql.Date(order.getCreateTime().getTime()));
        pstmt.setDate(7, new java.sql.Date(order.getUpdateTime().getTime()));
        pstmt.setInt(8, order.getGoodsId());

        int count=pstmt.executeUpdate();
        return count;
    }

    @Override
    public int delete(Integer goodsId) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;

        String sql = "delete from `order` where goods_id=?";
        pstmt = conn.prepareStatement(sql);

        pstmt.setInt(1, goodsId);
        int count=pstmt.executeUpdate();
        return count;
    }
}
